import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisSetDemo {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.flushAll();
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
            test6(jedis);
        }
    }

    private static void test6(Jedis jedis) {
        System.out.println("sinterstore");

        jedis.sadd("k1", "1", "2", "3");
        jedis.sadd("k2", "2", "3", "4");

        System.out.println("count: " + jedis.sinterstore("k3", "k1", "k2"));
        System.out.println("result: " + jedis.smembers("k3"));
    }

    private static void test5(Jedis jedis) {
        System.out.println("sinter");

        jedis.sadd("k1", "1", "2", "3");
        jedis.sadd("k2", "2", "3", "4");

        Set<String> result = jedis.sinter("k1", "k2");
        System.out.println("result: " + result);
    }

    private static void test4(Jedis jedis) {
        System.out.println("spop");

        jedis.sadd("k1", "111", "222", "333");
        System.out.println("result: " + jedis.spop("k1"));
        System.out.println("result: " + jedis.spop("k1"));
        System.out.println("result: " + jedis.spop("k1"));
        System.out.println("result: " + jedis.spop("k1"));
    }

    private static void test3(Jedis jedis) {
        System.out.println("sismember");

        jedis.sadd("k1", "1", "2", "3");
        System.out.println("'1': " + jedis.sismember("k1", "1"));
        System.out.println("'4': " + jedis.sismember("k1", "4"));
    }

    private static void test2(Jedis jedis) {
        System.out.println("scard");

        jedis.sadd("k1", "1", "2", "3");
        System.out.println("count: " + jedis.scard("k1"));
    }

    private static void test1(Jedis jedis) {
        System.out.println("sadd and smembers");

        jedis.sadd("k1", "111", "222", "333");
        System.out.println("result: " + jedis.smembers("k1"));
    }
}
